package com.service.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.service.admin.base.controller.rep.ConfigGroupRep;
import com.service.admin.base.controller.rep.ConfigPageRep;
import com.service.admin.base.controller.rep.ConfigRep;
import com.service.admin.base.controller.req.SysConfigModifyReq;
import com.service.admin.base.service.ConfigService;
import com.service.core.constant.BusinessStatus;
import com.service.core.exception.CommonException;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.mapper.SysConfigMapper;
import com.service.dao.model.SysConfig;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2018</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019-04-06</dd>
 * </dl>
 */
@Slf4j
@Service
public class ConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ConfigService {

    private Integer defaultNumberError = 5;

    @Override
    public SysConfig getConfigByName(String name) {
        return baseMapper.selectByName(name);
    }

    @Override
    public ConfigRep getConfigRepByName(String name) {
        SysConfig config = this.getConfigByName(name);
        return ObjectUtils.isEmpty(config) ? null : ModelMapperUtil.map(config, ConfigRep.class);
    }

    @Override
    public List<SysConfig> getConfigByGroup(Integer group) {
        return list(new QueryWrapper<SysConfig>().lambda()
                .eq(SysConfig::getConfGroup, group)
                .orderByAsc(SysConfig::getConfSort));
    }

    @Override
    public List<ConfigRep> getConfigRepByGroup(Integer group) {
        return this.getConfigByGroup(group).stream().map(item -> ModelMapperUtil.map(item, ConfigRep.class)).collect(Collectors.toList());
    }

    @Override
    public Map<String, SysConfig> getConfigMapByGroup(Integer group) {
        return getConfigByGroup(group).stream().collect(Collectors.toMap(SysConfig::getConfName, item -> item));
    }

    @Override
    public Map<String, String> getConfig2MapByGroup(Integer group) {
        return getConfigByGroup(group).stream().collect(Collectors.toMap(SysConfig::getConfName, SysConfig::getConfValue));
    }

    @Override
    public List<ConfigGroupRep> listGroup() {
        return Stream.of(ConfigConstants.Group.values()).map(item -> {
            ConfigGroupRep groupRep = new ConfigGroupRep();
            groupRep.setGroupCode(item.getCode());
            groupRep.setGroupDesc(item.getName());
            groupRep.setGroupName(item.toString());
            return groupRep;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ConfigPageRep> listGroupConfig(Integer group) {
        return getConfigByGroup(group).stream().map(item -> ModelMapperUtil.map(item, ConfigPageRep.class)).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyConfig(SysConfigModifyReq modifyReq) {

        if(ObjectUtils.isEmpty(getById(modifyReq.getId()))){
            throw new CommonException("不存在此系统参数配置");
        }
        SysConfig sysConfig = ModelMapperUtil.map(modifyReq, SysConfig.class);
        updateById(sysConfig);
    }


    @Override
    public String getETHNodeUrl(){
        SysConfig sysConfig = baseMapper.selectByName(ConfigConstants.ETH_NODE_RPC);
        return sysConfig.getConfValue();
    }


    @Override
    public String getETHCoinbase(){
        SysConfig sysConfig = baseMapper.selectByName(ConfigConstants.ETH_COINBASE);
        return sysConfig.getConfValue();
    }


    @Override
    public SysConfig getERC20USDT(){
        return baseMapper.selectByName(ConfigConstants.ERC20_USDT);
    }


    @Override
    public SysConfig getEXDF(){
        return baseMapper.selectByName(ConfigConstants.EXDF_CONTRACT);
    }
}
